﻿using System;
using System.Collections.Specialized;
using System.Web.UI;
using System.Web.UI.WebControls;
using Google.Translates.Core;

namespace ml_test_google_trans.UserControls
{
    public partial class UcGoogleTransButton : UserControl
    {
        public bool IsShowParmas
        {
            get
            {
                return chIsShowParmas.Checked;
            }
            set
            {
                ddlSrcLangKey.Visible = ddlTrgLangKey.Visible = lblMemo.Visible = value;
            }
        }

        public string TrgetLanguageKey
        {
            get { return ddlTrgLangKey.SelectedValue; }
            set
            {
                ddlTrgLangKey.SelectedValue = value;
            }
        }

        public string SourceLanguageKey
        {
            get { return ddlSrcLangKey.SelectedValue; }
            set
            {
                ddlSrcLangKey.SelectedValue = value;
            }
        }

        public string TransElementId
        {
            get { return hdnTransElementId.Value; }
            set { hdnTransElementId.Value = value; }
        }

        public string ResultElementId
        {
            get { return hdnResultElementId.Value; }
            set { hdnResultElementId.Value = value; }
        }

        protected void SetDefaultLangItems()
        {
            var nvcLangKeyInfo = new NameValueCollection()
                                     {
                                         {"en","English"},
                                         {"fr","Finnish"},
                                         {"es","Spanish"},
                                         {"vi","Vietnamese"},
                                         {"zh-CN","中文(简体)"},
                                         {"zh-TW","中文(繁體)"}
                                     };
            ddlSrcLangKey.Items.Clear();
            ddlTrgLangKey.Items.Clear();

            ddlSrcLangKey.Items.Add(new ListItem("Auto", ""));
            for(var i=0;i<nvcLangKeyInfo.Count;i++)
            {
                ddlSrcLangKey.Items.Add(new ListItem(nvcLangKeyInfo.Get(i), nvcLangKeyInfo.GetKey(i)));
                ddlTrgLangKey.Items.Add(new ListItem(nvcLangKeyInfo.Get(i), nvcLangKeyInfo.GetKey(i)));
            }

            
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                SetDefaultLangItems();
            }
        }

        protected void chIsShowParmas_CheckedChanged(object sender, EventArgs e)
        {
            IsShowParmas = chIsShowParmas.Checked;
        }

        protected void btnTranslates_Click(object sender, EventArgs e)
        {
            var transObj = new TranslatorByGoogleLib();
            if (hdnResultElementId.Value.Length == 0 || hdnTransElementId.Value.Length == 0) return;
            var srcControl = Page.FindControl(hdnTransElementId.Value);
            var trgControl = Page.FindControl(hdnResultElementId.Value);
            TextBox txtSrc = null,txtTrg=null;
            if(srcControl != null && srcControl is TextBox && trgControl != null && trgControl is TextBox)
            {
                txtSrc = srcControl as TextBox;
                txtTrg = trgControl as TextBox;
                var trgSentence = transObj.GetTranslationsByLongSentence(txtSrc.Text,ddlSrcLangKey.SelectedValue,ddlTrgLangKey.SelectedValue);
                txtTrg.Text = System.Web.HttpUtility.HtmlDecode(trgSentence);
            }
            
        }

    }
}